package com.stop.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.stop.R;
import com.stop.activity.adapter.SalasArrayAdapter;
import com.stop.controller.StopController;


public class SalasActivity extends Activity {

	private ListView lstLinguagens;
	private List<JSONObject> salas;
	private SalasArrayAdapter adapter;
	
	private ProgressDialog dialog;

	
	protected void onRestart() {
		dialog = ProgressDialog.show(SalasActivity.this, "", "Loading. Please wait...", true);
		new IniciarSalas().execute();
		super.onRestart();
	}
	
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		dialog = ProgressDialog.show(SalasActivity.this, "", "Loading. Please wait...", true);
		new IniciarSalas().execute();
		super.onRestoreInstanceState(savedInstanceState);
	}
	
	/*
	 * ao iniciar a activity
	 */
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_salas);
		
		salas = new ArrayList<JSONObject>();
		
		// Passamos a lista de exemplo para gerar nosso adpater
		adapter = new SalasArrayAdapter(getApplicationContext(), R.layout.activity_salas_adapter, salas);

		// Buscando o elemento Listview da nossa interface principal interface
		lstLinguagens = (ListView) findViewById(R.id.lstSalas);
		// Setando o adapter em nossa ListView
		lstLinguagens.setAdapter(adapter);

		// Setando callback ao selecionar um item da lista
		lstLinguagens.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				try {
					JSONObject sala = salas.get(position);
					Intent salaInfo = new Intent(getBaseContext(), SalaActivity.class);
					salaInfo.putExtra("sala", sala.toString());
					startActivityForResult(salaInfo, 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});
		
		
		dialog = ProgressDialog.show(SalasActivity.this, "", "Loading. Please wait...", true);
	}
	
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		
		new IniciarSalas().execute();
	}

	/*
	 * iniciar o menu principal
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		
		return super.onCreateOptionsMenu(menu);
	}
	
	/*
	 * quando selecionar uma opcao do menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.menu_nova_sala:
			Intent myIntent = new Intent(this.getBaseContext(), CriarSalaActivity.class);
			startActivityForResult(myIntent, 0);
			break;
		case R.id.menu_config:
			
            
			break;
		default:
			break;
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	/*
	 * carregar a lista de salas em background
	 */
	private class IniciarSalas extends AsyncTask<Void, Void, Void> {

		private JSONObject response;

		protected Void doInBackground(Void... params) {
			try {
				StopController.getInstancia().login();
				response = StopController.getInstancia().salas();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(Void result) {

			try {
				JSONArray salasArray = response.getJSONArray("salas");
				
				salas.clear();
				
				for (int i = 0; i < salasArray.length(); i++) {
					salas.add(salasArray.getJSONObject(i));
				}
				
				//atualizar a lista
				adapter.notifyDataSetChanged();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			dialog.cancel();
			
		}

	}

}
